/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-05-02 22:59:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-01 11:56:22
 * @FilePath: /pipe/Shm_ipc/ShmServer.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Comm.hpp"
#include <unistd.h>
#include"Fifo.hpp"


int main()
{
    //读端
    //0.引入管道
    Fifo fifo;
    Sync syn;
    
    // 1.创建key
    key_t key = GetShmKey();
    cout << "key:" << Tohex(key) << endl;
    // 2.创建共享内存
    int shmid = CreateShm(key, defaultsize);
    cout << "shmid:" << shmid << endl;
    // ShmDebug(shmid);
    // 挂接
    char *addr = (char *)ShmAttach(shmid);
    cout << "Attach shm success, addr:" << Tohex((uint64_t)addr) << endl;

    syn.OpenReadOrDie();
    cout<<"Ready to read......"<<endl;
    sleep(1);
    // 进行通信
    for (;;)
    {       
        if(!syn.Wait()) break;
        cout << "shm content:" << addr << endl; 
    }

    // 去挂接
    Detach(addr);
   std::cout << "Detach shm success, addr: " << Tohex((uint64_t)addr) << std::endl;

    // 3.删除共享内存
    DelShm(shmid);
    return 0;
}